import myAxios from './myAxios'
///import { useNavigate}  from 'react-router-dom'
//import {BASE_URL} from '../config/index'
import { URL, meshLog, SPRemove, SPGet, SPSet } from '../config'
import axios from "axios"
import { exportTimeStr } from '../utils/dataUtils'
// axios..post('http://localhost:3000/login', { "deviceName": "iPhone max", "deviceNo": "F1A53188C48BD4ACCEAD53725C
// )
// .then((result) => {
// meshLog(res
// meshLog(11);
// })
// .catch((resaon) => {
// meshLog(resao
// meshLog(1);
// }) 这种方式是直接在组件中发
// export const reqLogin = (passwd, email) => myAxios.post('/spring/login', { passwd, email })
// export const homeDeng = (token, email, meshUUID, addr, opCode, value) => myAxios.post('/spring/light', { token, email, meshUUID, addr, opCode, value })
// export const quanKong = (token, email, meshUUID, addr, opCode) => myAxios.post('/spring/light', { token, email, meshUUID, addr, opCode })
export const reqLogin = (maddr, pwd, platform, deviceNo) => myAxios.post(URL + '/login', { maddr, pwd, platform, deviceNo })
export const sendEmailVerificationCode = (maddr) => myAxios.post(URL + '/sendEmailVerificationCode', { maddr })
export const verifyAuth = (maddr, code, pwd) => myAxios.post(URL + '/verifyAuth', { maddr, code, pwd })
export const building = (maddr, deviceNo, token) => myAxios.post(URL + '/building', { maddr, deviceNo, token })
export const mesh = (maddr, deviceNo, token, buildingUUID) => myAxios.post(URL + '/mesh', { maddr, deviceNo, token, buildingUUID })
export const deleteuser = (maddr, deviceNo, token) => myAxios.post(URL + '/deleteuser', { maddr, deviceNo, token })


export const homeDeng = (token, email, meshUUID, addr, opCode, value) => myAxios.post(URL + '/light', { token, email, meshUUID, addr, opCode, value })
export const quanKong = (token, email, meshUUID, addr, opCode) => myAxios.post(URL + '/light', { token, email, meshUUID, addr, opCode })
export const getversion = (email, token, meshUUID) => myAxios.post(URL + '/getversion', { email, token, meshUUID })
export const getmeshInfo = (email, token, meshUUID) => myAxios.post(URL + '/getMeshInfo', { email, token, meshUUID })
export const meshinfoUpdate = (email, token, meshUUID, isSelected) => myAxios.post(URL + '/update_mesh_info', { email, token, meshUUID, isSelected })
export const uploadFileNetworking = (info) => {
    let version = parseInt(SPGet("version"));
    let filename = exportTimeStr(); //20230223113453
    var blob = new Blob([info], { type: "text/plain;charset=UTF-8;" });
    var file = new File([blob], filename + ".json", { type: "text/plain; charset = UTF-8;" });
    const formdata = new FormData();
    formdata.append("email", SPGet("email"))
    formdata.append("passwd", SPGet("passwd"))
    formdata.append("file", file)
    formdata.append("token", SPGet("token"))
    formdata.append("meshUUID", SPGet("meshUUID"))
    formdata.append("version", version + '')
    formdata.append("type", '0');
    formdata.append("deviceNo", SPGet("deviceNo"));
    axios({
        url: URL + '/fileUpload',
        method: 'post',
        headers: { 'Content-Type': 'multipart/form-data' },
        data: formdata
    }).then(async (request) => {
        //{"ctl_status":0,"url":"http://54.177.220.133:3000/uploads/file/2023030309010120230303170100.json"}
        let obj = JSON.stringify(request.data);
        let info = JSON.parse(obj);
        //meshLog("wjk request" + JSON.stringify(request.data));
        if (info.ctl_status == 0) {
            SPSet('version', (version + 1) + '');
            const email1 = (SPGet("email"))
            const token1 = (SPGet("token"))
            let meshUUID1 = ''
            let resultt = await getmeshInfo(email1, token1, meshUUID1,);
            //meshLog("resultt", resultt.meshList);
            SPRemove('meshList');//删除loaclstorage上的meshList
            SPSet('meshList', JSON.stringify(resultt.meshList));//存到localStorage
            meshLog("index.js 19行 wjk 上传成功");
        }
    },
        error => {
            meshLog("wjk error" + JSON.stringify(error.data));
        }
    )
}
